package com.jwcao.common.cast.annotation.factory;

import com.jwcao.common.cast.annotation.CastSet;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * name: CastSetFactory description: TODO
 *
 * @author caojingwei date: 2023/3/24 22:37
 */
public class CastSetFactory {

    public static String[] get(String id, Method method) {
        CastSet[] castFields = method.getAnnotationsByType(CastSet.class);
        if (ArrayUtils.isNotEmpty(castFields)) {
            return Arrays.stream(castFields)
                .filter(c -> StringUtils.equals(c.id(), id))
                .map(CastSet::value)
                .map(f -> ArrayUtils.isNotEmpty(f) ? f : new String[]{method.getName()})
                .findAny()
                .orElse(null);
        }

        return null;
    }
}
